package ext.generic.util;

import com.ptc.core.components.export.table.ExportException;
import com.ptc.core.components.export.table.ExportFileType;
import com.ptc.core.components.export.table.ExportListToFileIFC;
import com.ptc.netmarkets.model.NmObjectHelper;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.util.misc.TempFileCleaner;

import java.io.*;

import java.net.URL;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import wt.log4j.LogR;

import wt.session.SessionHelper;

import wt.util.*;

// Referenced classes of package com.ptc.core.components.export.table:
//            ExportException, ExportListToFileIFC, ExportFileType

public abstract class ExportListWriter implements ExportListToFileIFC, Serializable
{

    public static final int EXPORT_ROW_LIMIT = 65535;
    public static final String MODEL_RESOURCE = "com.ptc.netmarkets.model.modelResource";
    private static final Logger log; 
    protected static final String LINE_TERMINATOR = System.getProperty("line.separator");
    protected static final String COMMA_DELIM = ",";
    protected static final String TAB_DELIM = "\t";
    protected File f;
    protected Writer out;
    private ExportFileType fileType;
    private String fileName;
    
    static
    {
        try
        {
            log = LogR.getLogger(ext.generic.util.ExportListWriter.class.getName());
        }
        catch (Exception exception)
        {
            throw new ExceptionInInitializerError(exception);
        }
    }
    
    public ExportListWriter()
    {
    }

    public static List parseStringToList(String s, String s1)
    {
        List list = null;
        if (s != null && s1 != null)
        {
            String as[] = s.split(Pattern.quote(s1));
            list = Arrays.asList(as);
        }
        return list;
    }

    public final void open(File file, NmCommandBean nmcommandbean) throws ExportException
    {
        try
        {
            f = createExportTempFile(file);
            TempFileCleaner.register(nmcommandbean.getRequest().getSession(), f);
            FileOutputStream fileoutputstream = new FileOutputStream(f);
            out = new OutputStreamWriter(fileoutputstream, "UTF8");
        }
        catch (Exception exception)
        {
            log.error(exception, exception);
            throw new ExportException(exception);
        }
    }

    public URL getDownloadUrl(NmCommandBean nmcommandbean) throws ExportException
    {
        try
        {
            return NmObjectHelper.constructOutputURL(f, getFileName());
        }
        catch (WTException wtexception)
        {
            log.error(wtexception, wtexception);
            throw new ExportException(wtexception);
        }
    }

    protected File createExportTempFile(File file) throws IOException, WTException
    {
        File file1 = File.createTempFile("exportList", ".tmp", file);
        return file1;
    }

    public abstract void writeColumnHeaders(String s, String s1) throws ExportException;

    public abstract void writeRowData(List list) throws ExportException;

    public abstract void writeHeader(String s) throws ExportException;

    public abstract void writeFooter(String s) throws ExportException;

    public void close() throws ExportException
    {
        try
        {
            out.flush();
            out.close();
        }
        catch (IOException ioexception)
        {
            log.error(ioexception, ioexception);
            throw new ExportException(ioexception);
        }
    }

    File getFile()
    {
        return f;
    }

    public void setFileType(ExportFileType exportfiletype)
    {
        fileType = exportfiletype;
    }

    public ExportFileType getFileType()
    {
        return fileType;
    }

    public String getFileName()
    {
        return fileName;
    }

    public void setFileName(String s)
    {
        fileName = s;
    }

    public void setOut(Writer writer)
    {
        out = writer;
    }

    public static final int getExportFullListLimit()
    {
        try
        {
            return WTProperties.getLocalProperties().getProperty("com.ptc.jca.exportRowLimit", 65535);
        }
        catch (IOException ioexception)
        {
            log.error("IOException when getting the table export limit", ioexception);
        }
        return 65535;
    }

    public static final String getOverLimitMessage() throws WTException
    {
        String s = WTMessage.getLocalizedMessage("com.ptc.netmarkets.model.modelResource", "16", new String[]
                { String.valueOf(getExportFullListLimit()) }, SessionHelper.getLocale());
        return s;
    }

}
